package com.googlecode.mailnet.math.distribution;

import com.googlecode.mailnet.math.RandomUtil;

/**
 * @author Iskander Akishev
 */
class GammaDistribution implements Distribution {
    private final int k;
    private final double scale;

    GammaDistribution(double k, double scale) {
        if (scale <= 0) {
            throw new IllegalArgumentException("scale must be positive (" + scale + ")");
        }
        this.scale = scale;
        if (Math.abs(Math.rint(k) - k) > 1e-10) {
            throw new IllegalArgumentException("k must be integer (" + k + ")");
        }
        this.k = (int) Math.round(k);
        if (this.k <= 0) {
            throw new IllegalArgumentException("k must be positive (" + this.k + ")");
        }
    }

    public double getValue() {
        double v = 0;
        for (int i = 0; i < k; i++) {
            v -= Math.log(RandomUtil.nextDouble());
        }
        return scale * v;
    }
}
